//#include<stdio.h>
//#include <stdlib.h>
//
//
//struct TreeNode {
//	int val;
//	struct TreeNode* left;
//	struct TreeNode* right;
//	
//};
//
//
//int _sumOfLeftLeaves(struct TreeNode* root)
//{
//    if (root == NULL)
//        return  0;
//
//    if (root->left != NULL && root->left->right == NULL && root->left->left == NULL)
//    {
//        return root->left->val + _sumOfLeftLeaves(root->right) + _sumOfLeftLeaves(root->left);
//    }
//    else
//    {
//        return _sumOfLeftLeaves(root->right) + _sumOfLeftLeaves(root->left);
//    }
//}
//
//int sumOfLeftLeaves(struct TreeNode* root) {
//    int ret = _sumOfLeftLeaves(root);
//    printf("%d", ret);
//    return ret;
//}
//
//int main()
//{
//    struct TreeNode* Node1 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
//    struct TreeNode* Node2 = (struct TreeNode*)malloc(sizeof(struct TreeNode));
//    Node1->left = Node2;
//    Node1->right = NULL;
//    Node1->val = 1;
//    Node2->left = Node2->right = NULL;
//    Node2->val = 2;
//
//    sumOfLeftLeaves(Node1);
//    return 0;
//}

#include <stdio.h>
#include <stdlib.h>
typedef struct ListNode
{
    int val;
    struct ListNode* next;
}ListNode;

int main() {
    int n = 5;
    int m = 2;

    struct ListNode** arr = (struct ListNode**)malloc(sizeof(struct ListNode*) * n);

    for (int i = 0; i < n; i++)
    {
        arr[i] = (struct ListNode*)malloc(sizeof(struct ListNode));
    }

    for (int i = 0; i < n; i++)
    {
        arr[i]->val = i + 1;
        if (i < n - 1)
            arr[i]->next = arr[i + 1];
        else
            arr[i]->next = arr[0];
    }
    struct ListNode* head = arr[0];
    struct ListNode* cur = head;
    int i = 1;

    while (cur->next->val != cur->val)
    {
        if (i + 1 != m)
        {
            cur = cur->next;
            i++;
        }
        else
        {
            struct ListNode* next = cur->next->next;
            free(cur->next);
            cur->next = next;
            cur = cur->next;
            i = 1;
        }
    }
    printf("%d", cur->val);
    free(cur);
    return 0;
}